#include "AEInputManager.hpp"
#include "AELogManager.hpp"

using namespace OIS;
using namespace arete::io;

BEGIN_ARETE_NAMESPACE(input)

	AEInputManager::AEInputManager(void) :
	m_inputmanager(NULL),
	m_keyboard(NULL),
	m_mouse(NULL),
	m_double_click_interval(300.0f),
	m_key_repeat_interval(300.0f),
	AERepeatKey(m_key_repeat_interval,m_key_repeat_interval + 100)
{
}

AEInputManager::~AEInputManager(void)
{
	if(m_inputmanager)
	{
		if(m_mouse)
		{
			m_inputmanager->destroyInputObject(m_mouse);
			m_mouse = NULL;
		}

		if(m_keyboard)
		{
			m_inputmanager->destroyInputObject(m_keyboard);
			m_keyboard = NULL;
		}

		m_inputmanager->destroyInputSystem(m_inputmanager);
		m_inputmanager = NULL;
	}
}

void AEInputManager::initialize(Ogre::RenderWindow* _win)
{
	AELogManager::log(ELL_NONE,"*****************************************");
	AELogManager::log(ELL_NONE,"*** --- Initializing InputManager --- ***");
	AELogManager::log(ELL_NONE,"*****************************************");

	std::ostringstream oss;
	ParamList params;

	if(!_win->isFullScreen())
	{
		params.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND" )));
		params.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCLUSIVE")));
		params.insert(std::make_pair(std::string("w32_keyboard"), std::string("DISCL_FOREGROUND")));
		params.insert(std::make_pair(std::string("w32_keyboard"), std::string("DISCL_NONEXCLUSIVE")));
	}

	size_t hwnd = 0;
	_win->getCustomAttribute("WINDOW",&hwnd);
	oss << static_cast<unsigned int>(hwnd);

	params.insert(std::make_pair(std::string("WINDOW"), std::string(oss.str())));

	m_inputmanager = InputManager::createInputSystem(params);

	if(m_inputmanager->getNumberOfDevices(OISKeyboard) > 0)
	{
		m_keyboard = static_cast<Keyboard*>(m_inputmanager->createInputObject(OISKeyboard,true));
		m_keyboard->setEventCallback(this);
	}

	if(m_inputmanager->getNumberOfDevices(OISMouse) > 0)
	{
		m_mouse = static_cast<Mouse*>(m_inputmanager->createInputObject(OISMouse,true));
		m_mouse->setEventCallback(this);

		unsigned int width, height, depth;
		int left, top;

		_win->getMetrics(width,height,depth,left,top);

		MouseState& mouseState = const_cast<MouseState&>(m_mouse->getMouseState());
		mouseState.width = width;
		mouseState.height = height;

		if(_win->isFullScreen())
		{
			mouseState.X.abs = width / 2;
			mouseState.Y.abs = height / 2;
		}
	}
}

void AEInputManager::update(float _delta)
{
	if( m_mouse ) {
		m_mouse->capture();
	}

	if( m_keyboard ) {
		m_keyboard->capture();
	}

	repeat(_delta);
}

void AEInputManager::windowResized( Ogre::RenderWindow* _rw )
{
	unsigned int width, height, depth;
	int left, top;

	_rw->getMetrics(width,height,depth,left,top);

	const MouseState& mouseState = m_mouse->getMouseState();
	mouseState.width = width;
	mouseState.height = height;
}

bool AEInputManager::keyPressed( const KeyEvent & _evt )
{
	begin(_evt);

	return true;
}

bool AEInputManager::keyReleased( const KeyEvent & _evt )
{
	end(_evt);

	return true;
}

bool AEInputManager::mouseMoved( const MouseEvent & _evt )
{
	return true;
}

bool AEInputManager::mousePressed( const MouseEvent & _evt, MouseButtonID _id )
{
	return true;
}

bool AEInputManager::mouseReleased( const MouseEvent & _evt, MouseButtonID _id )
{
	return true;
}

void AEInputManager::repeatKey( OIS::KeyCode,unsigned int )
{
}

OIS::InputManager* AEInputManager::getInputManager( void )
{
	return m_inputmanager;
}

OIS::Keyboard* AEInputManager::getKeyboard( void )
{
	return m_keyboard;
}

OIS::Mouse* AEInputManager::getMouse( void )
{
	return m_mouse;
}

END_ARETE_NAMESPACE